﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace InformationExtraction.Common
{
    public enum InformationType
    {
        None,
        Name,
        Homepage,
        Location,
        Acronym,
        Date,
        CRDate,
        SubmissionDate,
        NotificationDate
    }

    public enum TagType
    {
        NameL,
        NameR,
        HomePageL,
        HomePageR,
        LocationL,
        LocationR,
        DateL,
        DateR,
        AcronymL,
        AcronymR,
        CRDateL,
        CRDateR,
        SubmissionDateL,
        SubmissionDateR,
        NotificationDateL,
        NotificationDateR,
        None
    }

    class Tag
    {
        private const string NameL = @"<[a-zA-Z]*name>";
        private const string NameR = @"</[a-zA-Z]*name>";

        private const string HomePageL = @"<[a-zA-Z]*homepage>";
        private const string HomePageR = @"</[a-zA-Z]*homepage>";

        private const string LocationL = @"<[a-zA-Z]*location>";
        private const string LocationR = @"</[a-zA-Z]*location>";

        private const string DateL = @"<[workshop|conference]+date>";
        private const string DateR = @"</[workshop|conference]+date>";

        private const string AcronymL = @"<[a-zA-Z]*acronym>";
        private const string AcronymR = @"</[a-zA-Z]*acronym>";

        private const string CRDateL = @"<[a-zA-Z]*camerareadycopydate>";
        private const string CRDateR = @"</[a-zA-Z]*camerareadycopydate>";

        private const string SubmissionDateL = @"<[a-zA-Z]*submissiondate>";
        private const string SubmissionDateR = @"</[a-zA-Z]*submissiondate>";

        private const string NotificationDateL = @"<[a-zA-Z]*notification[a-zA-Z]*date>";
        private const string NotificationDateR = @"</[a-zA-Z]*notification[a-zA-Z]*date>";

        public static TagType MatchTag(string s, ref string preRemain, ref string postRemain)
        {
            if (MatchTag(NameL, s, ref preRemain, ref postRemain))
                return TagType.NameL;
            else if (MatchTag(NameR, s, ref preRemain, ref postRemain))
                return TagType.NameR;
            else if (MatchTag(DateL, s, ref preRemain, ref postRemain))
                return TagType.DateL;
            else if (MatchTag(DateR, s, ref preRemain, ref postRemain))
                return TagType.DateR;
            else if (MatchTag(HomePageL, s, ref preRemain, ref postRemain))
                return TagType.HomePageL;
            else if (MatchTag(HomePageR, s, ref preRemain, ref postRemain))
                return TagType.HomePageR;
            else if (MatchTag(LocationL, s, ref preRemain, ref postRemain))
                return TagType.LocationL;
            else if (MatchTag(LocationR, s, ref preRemain, ref postRemain))
                return TagType.LocationR;
            else if (MatchTag(DateL, s, ref preRemain, ref postRemain))
                return TagType.DateL;
            else if (MatchTag(DateR, s, ref preRemain, ref postRemain))
                return TagType.DateR;
            else if (MatchTag(AcronymL, s, ref preRemain, ref postRemain))
                return TagType.AcronymL;
            else if (MatchTag(AcronymR, s, ref preRemain, ref postRemain))
                return TagType.AcronymR;
            else if (MatchTag(CRDateL, s, ref preRemain, ref postRemain))
                return TagType.CRDateL;
            else if (MatchTag(CRDateR, s, ref preRemain, ref postRemain))
                return TagType.CRDateR;
            else if (MatchTag(SubmissionDateL, s, ref preRemain, ref postRemain))
                return TagType.SubmissionDateL;
            else if (MatchTag(SubmissionDateR, s, ref preRemain, ref postRemain))
                return TagType.SubmissionDateR;
            else if (MatchTag(NotificationDateL, s, ref preRemain, ref postRemain))
                return TagType.NotificationDateL;
            else if (MatchTag(NotificationDateR, s, ref preRemain, ref postRemain))
                return TagType.NotificationDateR;
            else 
                return TagType.None;
        }

        private static bool MatchTag(string regPattern, string content, ref string preRemain, ref string postRemain)
        {
            preRemain = "";
            postRemain = "";

            Regex reg = new Regex(regPattern);
            MatchCollection mc = reg.Matches(content);
            
            if (mc.Count > 0)
            {
                Match m = mc[0];
                preRemain = content.Substring(0, m.Index);
                postRemain = content.Substring(m.Index + m.Length);
                return true;
            }

            return false;
        }

    
    }
}
